package com.taotao.tools.codec.sha;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.xml.bind.DatatypeConverter;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SHA512Utils {
    public static String encrypt(byte[] input) throws NoSuchAlgorithmException {
        // 获取SHA-512算法的实例
        MessageDigest sha512 = MessageDigest.getInstance("SHA-512");
        // 设置要加密的数据
        sha512.update(input);
        // 计算哈希值
        byte[] hash = sha512.digest();
        // 将哈希值转化为十六进制字符串
        String encrypted = DatatypeConverter.printHexBinary(hash);
        return encrypted;
    }

    public static String encrypt(String input) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        return encrypt(input.getBytes("UTF-8"));
    }

    public static String encrypt(File input) throws NoSuchAlgorithmException {
        return encrypt(FileUtils.readFile(input));
    }


    public static void main(String[] args) {
        try {
            String input = "Hello World";
            String encrypted = encrypt(input);
            System.out.println("加密前的数据：" + input);
            System.out.println("加密后的数据：" + encrypted);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
